﻿Imports MeoTom.Core.BLL
Imports Microsoft.ApplicationBlocks.Data

Namespace DAL
    Public Class ArticleProvider
        Inherits DataAccess
        Private util As New MeoTom.Core.Utilities.Common()

        Public Sub New()

        End Sub

        Public Function Create(ByVal a As Articles) As SqlClient.SqlDataReader
            Dim param(17) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@Title", SqlDbType.NVarChar, 550)
            param(0).Value = a.Title
            param(1) = New SqlClient.SqlParameter("@IsPicture", SqlDbType.Int, 4)
            param(1).Value = a.IsPicture
            param(2) = New SqlClient.SqlParameter("@Abstract", SqlDbType.NVarChar, 1000)
            param(2).Value = a.Abstract
            param(3) = New SqlClient.SqlParameter("@Approved", SqlDbType.Int, 4)
            param(3).Value = a.Approved
            param(4) = New SqlClient.SqlParameter("@CommentsEnabled", SqlDbType.Int, 4)
            param(4).Value = a.CommentEnabled
            param(5) = New SqlClient.SqlParameter("@ExpireDate", SqlDbType.DateTime)
            param(5).Value = a.ExpireDate
            param(6) = New SqlClient.SqlParameter("@ReleaseDate", SqlDbType.DateTime)
            param(6).Value = a.ReleaseDate
            param(7) = New SqlClient.SqlParameter("@ImagePath", SqlDbType.VarChar, 150)
            param(7).Value = a.ImagePath
            param(8) = New SqlClient.SqlParameter("@IsMedia", SqlDbType.Int, 4)
            param(8).Value = a.IsMedia
            param(9) = New SqlClient.SqlParameter("@TitleNone", SqlDbType.VarChar, 550)
            param(9).Value = a.TitleNone
            param(10) = New SqlClient.SqlParameter("@ArticlePassword", SqlDbType.VarChar, 150)
            param(10).Value = a.ArticlePassword
            param(11) = New SqlClient.SqlParameter("@PostVisible", SqlDbType.Int, 4)
            param(11).Value = a.PostVisible
            param(12) = New SqlClient.SqlParameter("@ArticlePath", SqlDbType.NVarChar, 600)
            param(12).Value = a.ArticlePath
            param(13) = New SqlClient.SqlParameter("@UserID", SqlDbType.Int, 4)
            param(13).Value = a.UserID
            param(14) = New SqlClient.SqlParameter("@CreateDate", SqlDbType.DateTime)
            param(14).Value = a.CreatedDate
            param(15) = New SqlClient.SqlParameter("@IsTopPage", SqlDbType.Int, 4)
            param(15).Value = a.IsTopPage
            param(16) = New SqlClient.SqlParameter("@IsVote", SqlDbType.Int, 4)
            param(16).Value = a.IsVote

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_Create", param)
        End Function

        Public Function CreateProduct(ByVal a As Articles) As SqlClient.SqlDataReader
            Dim param(9) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@Title", SqlDbType.NVarChar, 550)
            param(0).Value = a.Title
            param(1) = New SqlClient.SqlParameter("@ReleaseDate", SqlDbType.DateTime)
            param(1).Value = a.ReleaseDate
            param(2) = New SqlClient.SqlParameter("@ImagePath", SqlDbType.VarChar, 150)
            param(2).Value = a.ImagePath
            param(3) = New SqlClient.SqlParameter("@TitleNone", SqlDbType.VarChar, 550)
            param(3).Value = a.TitleNone
            param(4) = New SqlClient.SqlParameter("@PostVisible", SqlDbType.Int, 4)
            param(4).Value = a.PostVisible
            param(5) = New SqlClient.SqlParameter("@ArticlePath", SqlDbType.NVarChar, 600)
            param(5).Value = a.ArticlePath
            param(6) = New SqlClient.SqlParameter("@CreateDate", SqlDbType.DateTime)
            param(6).Value = a.CreatedDate
            param(7) = New SqlClient.SqlParameter("@Price", SqlDbType.Int, 4)
            param(7).Value = Integer.Parse(a.Price.Replace(".", ""))
            param(8) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(8).Value = Integer.Parse(Configs.SiteID)

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Product_Create", param)
        End Function

        'Public Sub Update(ByVal a As Articles)
        '    Dim param(4) As SqlClient.SqlParameter
        '    param(0) = New SqlClient.SqlParameter("@Title", SqlDbType.NVarChar, 550)
        '    param(0).Value = a.Title
        '    param(1) = New SqlClient.SqlParameter("@ImagePath", SqlDbType.VarChar, 150)
        '    param(1).Value = a.ImagePath
        '    param(2) = New SqlClient.SqlParameter("@Price", SqlDbType.Int, 4)
        '    param(2).Value = Integer.Parse(a.Price.Replace(".", ""))
        '    param(3) = New SqlClient.SqlParameter("@ArticlesID", SqlDbType.Int, 4)
        '    param(3).Value = a.ArticlesID

        '    SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Product_Update", param)
        'End Sub

        Public Sub Update(ByVal a As Articles)
            Dim param(16) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@Title", SqlDbType.NVarChar, 550)
            param(0).Value = a.Title
            param(1) = New SqlClient.SqlParameter("@IsPicture", SqlDbType.Int, 4)
            param(1).Value = a.IsPicture
            param(2) = New SqlClient.SqlParameter("@Abstract", SqlDbType.NVarChar, 3000)
            param(2).Value = a.Abstract
            param(3) = New SqlClient.SqlParameter("@Approved", SqlDbType.Int, 4)
            param(3).Value = a.Approved
            param(4) = New SqlClient.SqlParameter("@CommentsEnabled", SqlDbType.Int, 4)
            param(4).Value = a.CommentEnabled
            param(5) = New SqlClient.SqlParameter("@ExpireDate", SqlDbType.DateTime)
            param(5).Value = a.ExpireDate
            param(6) = New SqlClient.SqlParameter("@ReleaseDate", SqlDbType.DateTime)
            param(6).Value = a.ReleaseDate
            param(7) = New SqlClient.SqlParameter("@ImagePath", SqlDbType.VarChar, 150)
            param(7).Value = a.ImagePath
            param(8) = New SqlClient.SqlParameter("@ArticlesID", SqlDbType.Int, 4)
            param(8).Value = a.ArticlesID
            param(9) = New SqlClient.SqlParameter("@IsMedia", SqlDbType.Int, 4)
            param(9).Value = a.IsMedia
            param(10) = New SqlClient.SqlParameter("@TitleNone", SqlDbType.VarChar, 550)
            param(10).Value = a.TitleNone
            param(11) = New SqlClient.SqlParameter("@ArticlePassword", SqlDbType.VarChar, 150)
            param(11).Value = a.ArticlePassword
            param(12) = New SqlClient.SqlParameter("@PostVisible", SqlDbType.Int, 4)
            param(12).Value = a.PostVisible
            param(13) = New SqlClient.SqlParameter("@ArticlePath", SqlDbType.NVarChar, 600)
            param(13).Value = a.ArticlePath
            param(14) = New SqlClient.SqlParameter("@IsTopPage", SqlDbType.Int, 4)
            param(14).Value = a.IsTopPage
            param(15) = New SqlClient.SqlParameter("@IsVote", SqlDbType.Int, 4)
            param(15).Value = a.IsVote

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Article_Update", param)
        End Sub

        Public Sub Delete(ByVal a As Articles)
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticlesID", SqlDbType.Int, 4)
            param(0).Value = a.ArticlesID
            param(1) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(1).Value = Configs.SiteID

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Article_Delete", param)
        End Sub

        Public Sub UpdateArticlePath(ByVal a As Articles)
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticlePath", SqlDbType.NVarChar, 600)
            param(0).Value = a.ArticlePath
            param(1) = New SqlClient.SqlParameter("@ArticleID", SqlDbType.Int, 4)
            param(1).Value = a.ArticlesID

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Article_UpdatePath", param)
        End Sub

        Public Sub SetTempArticleCat(ByVal a As Articles, ByVal t As Integer)
            Dim param(3) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@CatID", SqlDbType.Int, 4)
            param(0).Value = a.CategoryId
            param(1) = New SqlClient.SqlParameter("@Type", SqlDbType.Int, 4)
            param(1).Value = t
            param(2) = New SqlClient.SqlParameter("@ArticleID", SqlDbType.Int, 4)
            param(2).Value = a.ArticlesID

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Article_SetTempArticleCat", param)
        End Sub

        Public Sub RemoveTempArticleCat(ByVal a As Articles)
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticleID", SqlDbType.Int, 4)
            param(0).Value = a.ArticlesID

            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Article_RemoveTempArticleCat", param)
        End Sub

        Public Function GetAll(ByVal a As Articles) As DataSet
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(0).Value = Configs.SiteID

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "Justin_Article_GetAll", param)
        End Function

        Public Function GetByTagInside(ByVal a As Articles) As DataSet
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@TagID", SqlDbType.Int, 4)
            param(0).Value = a.TagID
            param(1) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(1).Value = Configs.SiteID

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "Justin_Article_GetByTagInside", param)
        End Function

        Public Function GetByCat(ByVal a As Articles) As DataSet
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@CatID", SqlDbType.Int, 4)
            param(0).Value = a.CategoryId
            param(1) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(1).Value = Configs.SiteID

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "Justin_Article_GetByCat", param)
        End Function

        Public Function GetByCatInside(ByVal a As Articles) As SqlClient.SqlDataReader
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@CatID", SqlDbType.Int, 4)
            param(0).Value = a.CategoryId
            param(1) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(1).Value = Configs.SiteID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetByCat", param)
        End Function

        Public Function GetByCatInside2(ByVal a As Articles) As DataSet
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@CatID", SqlDbType.Int, 4)
            param(0).Value = a.CategoryId
            param(1) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(1).Value = Configs.SiteID

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "Justin_Article_GetByCat", param)
        End Function

        Public Function GetByEdit(ByVal a As Articles) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticleID", SqlDbType.Int, 4)
            param(0).Value = a.ArticlesID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetByEdit", param)
        End Function

        Public Function GetByID(ByVal a As Articles) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticlesID", SqlDbType.Int, 4)
            param(0).Value = a.ArticlesID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetByID", param)
        End Function

        Public Function GetOlder(ByVal a As Articles) As DataSet
            Dim param(3) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticleID", SqlDbType.Int, 4)
            param(0).Value = a.ArticlesID
            param(1) = New SqlClient.SqlParameter("@CatID", SqlDbType.Int, 4)
            param(1).Value = a.CategoryId
            param(2) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(2).Value = Configs.SiteID

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "Justin_Article_GetOlder", param)
        End Function

        Public Function GetOlder(ByVal ArticleID As Integer, ByVal CatID As Integer) As SqlClient.SqlDataReader
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticleID", SqlDbType.Int, 4)
            param(0).Value = ArticleID
            param(1) = New SqlClient.SqlParameter("@CatID", SqlDbType.Int, 4)
            param(1).Value = CatID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetOlder", param)
        End Function

        Public Function GetAllCat(ByVal a As Articles) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticlesID", SqlDbType.Int, 4)
            param(0).Value = a.ArticlesID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetAllCat", param)
        End Function

        Public Function GetAllTag(ByVal a As Articles) As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@ArticlesID", SqlDbType.Int, 4)
            param(0).Value = a.ArticlesID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetAllTag", param)
        End Function

        Public Sub UpdateViewCount(ByVal a As Articles)
            Try
                Dim param(1) As SqlClient.SqlParameter
                param(0) = New SqlClient.SqlParameter("@ArticleID", SqlDbType.Int, 4)
                param(0).Value = a.ArticlesID

                SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Article_UpdateCount", param)
            Catch ex As Exception
            End Try
        End Sub

        Public Sub UpdateViewCountMonth()
            SqlHelper.ExecuteNonQuery(strConn, CommandType.StoredProcedure, "Justin_Article_UpdateCountMonth")
        End Sub

        Public Function GetTop10Last() As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(0).Value = Configs.SiteID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetTop10Last", param)
        End Function

        Public Function GetTop10View() As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(0).Value = Configs.SiteID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetTop10View", param)
        End Function

        Public Function GetArchive() As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(0).Value = Configs.SiteID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_PostCount_GetAll", param)
        End Function

        Public Function GetTop20NoCat() As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(0).Value = Configs.SiteID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetTop20NoCat", param)
        End Function

        Public Function GetTopStory() As SqlClient.SqlDataReader
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(0).Value = Configs.SiteID

            Return SqlHelper.ExecuteReader(strConn, CommandType.StoredProcedure, "Justin_Article_GetTopStory", param)
        End Function

        Public Function GetArticle() As DataSet
            Dim param(1) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(0).Value = Configs.SiteID

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "Justin_Article_Get", param)
        End Function

        Public Function Find(ByVal strKeyWord As String) As DataSet
            Dim param(2) As SqlClient.SqlParameter
            param(0) = New SqlClient.SqlParameter("@KeyWord", SqlDbType.NVarChar, 1000)
            param(0).Value = "%" & strKeyWord & "%"
            param(1) = New SqlClient.SqlParameter("@SiteID", SqlDbType.Int, 4)
            param(1).Value = Configs.SiteID

            Return SqlHelper.ExecuteDataset(strConn, CommandType.StoredProcedure, "Justin_Article_Find", param)
        End Function
    End Class
End Namespace